package com.lottemall.item.service;

import com.lottemall.common.enums.ExceptionEnume;
import com.lottemall.common.exception.LtException;
import com.lottemall.item.mapper.CategoryMapper;
import com.lottemall.item.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * (
 * 品牌业务实现类
 * <p>
 * 为了偷懒，直接写业务实现类，
 */
@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 根据父节点id查询商品分类
     * @param pid
     * @return
     */
    public List<Category> queryCategoryListById(Long pid) {
        //创建对象传入将要查询的条件
        Category category = new Category();
        category.setParentId(pid);
        //将非空属性作为查询条件
        List<Category> list = categoryMapper.select(category);
        //判断是否为空
        if (CollectionUtils.isEmpty(list)) {
            throw new LtException(ExceptionEnume.CATEGORY_NOT_FOND);
        }
        return list;
    }


    /**
     * 根据一堆商品类型id，查询对应的商品类型实体集合
     * @param ids
     * @return
     */
    public List<Category> queryByIds(List<Long> ids){
        List<Category> categoryList = categoryMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(categoryList)) {
            throw new LtException(ExceptionEnume.CATEGORY_NOT_FOND);
        }
        return categoryList;
    }
}
